package cn.edu.zafu.bigdata.zyf;

import java.io.*;
import java.util.*;

// Student 类
class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    String id;
    String name;
    String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

// StudentManager 类
class StudentManager {
    private List<Student> students = new ArrayList<>();
    private String filename = "students.dat";

    public void addStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter student ID: ");
        String id = scanner.nextLine();
        for (Student s : students) {
            if (s.getId().equals(id)) {
                System.out.println("Student ID already exists. Please try again.");
                return;
            }
        }
        System.out.print("Enter student name: ");
        String name = scanner.nextLine();
        System.out.print("Enter student phone: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        students.add(student);
        saveStudents();
        System.out.println("Student added successfully.");
    }

    public void retrieveStudent() {
        Scanner scanner = new Scanner(System.in);
        String input = "";
        do {
            System.out.print("Enter student ID to retrieve (or 'QUIT' to exit): ");
            input = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(input)) {
                break;
            }
            Student student = findStudentById(input);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("Student not found.");
            }
        } while (!"QUIT".equalsIgnoreCase(input));
    }

    public void updateStudent() {
        Scanner scanner = new Scanner(System.in);
        String input = "";
        do {
            System.out.print("Enter student ID to update (or 'QUIT' to exit): ");
            input = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(input)) {
                break;
            }
            Student student = findStudentById(input);
            if (student != null) {
                System.out.print("Enter new name: ");
                String name = scanner.nextLine();
                System.out.print("Enter new phone: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                saveStudents();
                System.out.println("Student updated successfully.");
            } else {
                System.out.println("Student not found.");
            }
        } while (!"QUIT".equalsIgnoreCase(input));
    }

    public void deleteStudent() {
        Scanner scanner = new Scanner(System.in);
        String input = "";
        do {
            System.out.print("Enter student ID to delete (or 'QUIT' to exit): ");
            input = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(input)) {
                break;
            }
            Student student = findStudentById(input);
            if (student != null) {
                students.remove(student);
                saveStudents();
                System.out.println("Student deleted successfully.");
            } else {
                System.out.println("Student not found.");
            }
        } while (!"QUIT".equalsIgnoreCase(input));
    }

    private Student findStudentById(String id) {
        for (Student s : students) {
            if (s.getId().equals(id)) {
                return s;
            }
        }
        return null;
    }

    void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
            students = (List<Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            students = new ArrayList<>();
        }
    }
}

// Main 类
public class Main {
    public static void main(String[] args) {
        StudentManager manager = new StudentManager();
        manager.loadStudents();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // consume newline

            switch (choice) {
                case 1:
                    manager.addStudent();
                    break;
                case 2:
                    manager.retrieveStudent();
                    break;
                case 3:
                    manager.updateStudent();
                    break;
                case 4:
                    manager.deleteStudent();
                    break;
                case 5:
                    manager.saveStudents();
                    System.out.println("Exiting program.");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid choice. Please enter a number between 1 and 5.");
                    break;
            }
        }
    }
}